<template>
  <div class="Gifts">
    <div class="wrap">
      <div>全部礼品</div>
      <ul class="opt">
        <li>
          <strong>排序</strong>
          <span
            v-for="(item, index) in opt1"
            :key="index"
            @click="opt1Click(index, item.min, item.max)"
            :class="num1==index?'active':''"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="giftList" :max-length="100"></List>
      <Crumb></Crumb>
    </div>
  </div>
</template>

<script>
import Crumb from "../components/crumb.vue";
import List from "../components/index/List.vue";
import { giftAPI } from "@/request/api.js";
export default {
  name: "Gift",
  components: {
    List,
    Crumb,
  },
  data() {
    return {
      // 栏目 目前可提供的参数是 1->精品推荐 2-> 热门兑换 0->全部
      did: 0,
      // ( 1->实体商品 2->虚拟商品 0->全部 )
      type: 0,
      // 大于多少积分，最少是0
      min: 0,
      // 少于多少积分，最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      max: 0,
      // 商品关键词
      keyword: "",
      // 商品信息列表
      giftList: [],
      opt1: [
        { txt: "全部", min: 0, max: 10000 },
        { txt: "我能兑换的", min: 0, max: 10000 },
        { txt: "0-500分", min: 0, max: 500 },
        { txt: "500-1000分", min: 500, max: 1000 },
        { txt: "1000-1500分", min: 1000, max: 1500 },
        { txt: "1500-2500分", min: 1500, max: 2500 },
        { txt: "2500-6500分", min: 2500, max: 6500 },
        { txt: "6500-10000分", min: 6500, max: 10000 },
      ],
      // 当前样式标记
      num1: 0,
    };
  },
  // watch: {
  //   "$route.query.keyword": {
  //     handler() {
  //       giftAPI();
  //     },
  //   },
  // },

  mounted() {},

  methods: {
    opt1Click(num1, min, max) {
      this.num1 = num1, 
      this.min = min,
      this.max = max, 
      this.giftHS();
    },
    // 发送请求传参要重新把变量声明一遍
    // giftHS是搜索请求的函数
    async giftHS() {
         let res = await giftAPI({
          did:this.did,
          type:this.type,
          min:this.min,
          max:this.max,
          keyword:this.keyword
        })
        console.log(res.data);
        this.giftList=res.data
    },
  },
  created() {
    this.keyword = this.$route.query.keyword||''
    this.giftHS();
  },
};
</script>

<style lang="less">
@import "../assets/global.less";
.Gifts {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .opt {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active{
          color: #f12020;
        }
      }
    }
  }
}
</style>